﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;
using CSSoft;

namespace CSSoft.VNTower.Domain
{
    [Serializable]
    public class DailyReportItem : BaseItem
    {
        public List<DailyReportFileItem> ReportFiles { get; set; }
        public string Note { get; set; }
        public DateTime Date { get; set; }
        public UserItem Reporter { get; set; }
        public List<UserItem> ReportTo { get; set; }
        public List<UserItem> ReportCC { get; set; }
        public string LineManagerComment { get; set; }
        public string ReportStatus { get; set; }

        public DailyReportItem() { 
            //WorkingProjects = new List<ProjectItem>();
            ReportFiles = new List<DailyReportFileItem>();
            Reporter = new UserItem();
            ReportTo = new List<UserItem>();
            ReportCC = new List<UserItem>();
        }
        public void GetItem(SPListItem item)
        {
            Item = item;
            try
            {
                DateTime? date = CS2Convert.ToDateTime(Title, Constants.DATE_FORMAT);
                Date = date == null ? DateTime.Today : date.Value;
                Note = CS2Convert.ToString(Item["Note"]);
                Reporter = new UserItem { User = CS2Convert.ToSPUser(Item["Author"]) };

                LineManagerComment = CS2Convert.ToString(item["LineManagerComment"]);
                ReportStatus = CS2Convert.ToString(item["Status"]);

                SPFieldUserValueCollection reportTo = CS2Convert.ToSPFieldUserValueCollection(Item["ReportTo"]);
                if (reportTo != null)
                    foreach (SPFieldUserValue user in reportTo)
                        ReportTo.Add(new UserItem { User = user.User });
                SPFieldUserValueCollection reportCC = CS2Convert.ToSPFieldUserValueCollection(Item["ReportCC"]);
                if (reportCC != null)
                    foreach (SPFieldUserValue user in reportCC)
                        ReportCC.Add(new UserItem { User = user.User });
            }
            catch
            {
                Date = DateTime.Today;
                Reporter = new UserItem() { User = CS2Web.CurrentUser };
            }
        }
    }
}
